package com.crl.ssc.dic.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.ssc.dic.model.DicProvince;
import com.crl.ssc.dic.vo.DicProvinceSearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @description: 省份字典Dao
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/7/21 13:17
 */
@Repository
public class DicProvinceDao extends BaseDao<DicProvince, DicProvinceSearchVO> {

    /**
     * 新增
     *
     * @param dicProvince
     * @return
     */
    public int add(DicProvince dicProvince) {
        String sql = "INSERT INTO td_dic_province(province_code,province_name) " +
                "VALUES(:provinceCode,:provinceName)";
        return insert(sql, dicProvince);
    }


    /**
     * 修改
     *
     * @param dicProvince
     * @return
     */
    public int update(DicProvince dicProvince) {
        String sql = "UPDATE td_dic_province SET province_name=:provinceName WHERE province_code=:provinceCode";
        return update(sql, dicProvince);
    }

    /**
     * 删除
     *
     * @param provinceCode
     * @return
     */
    public int delete(String provinceCode) {
        String sql = "DELETE FROM td_dic_province WHERE province_code=?";
        return delete(sql, provinceCode);
    }


    /**
     * 列表
     *
     * @param dicProvinceSearchVO
     * @return
     */
    public List<DicProvince> list(DicProvinceSearchVO dicProvinceSearchVO) {
        String sql = "SELECT province_code,province_name FROM td_dic_province WHERE 1=1 ";
        sql += createSearchSQL(dicProvinceSearchVO);
        sql += " ORDER BY province_code ASC";
        sql = PageUtil.createOraclePageSQL(sql, dicProvinceSearchVO.getPage());
        return list(sql, dicProvinceSearchVO);
    }

    /**
     * 查询数据条数
     *
     * @param dicProvinceSearchVO
     * @return
     */
    public int count(DicProvinceSearchVO dicProvinceSearchVO) {
        String sql = "SELECT COUNT(province_code) FROM td_dic_province WHERE 1=1 ";
        sql += createSearchSQL(dicProvinceSearchVO);
        return count(sql, dicProvinceSearchVO);
    }

    /**
     * 获取单个
     *
     * @param provinceCode
     * @return
     */
    public DicProvince get(String provinceCode) {
        String sql = "SELECT province_code,province_name FROM td_dic_province WHERE province_code=?";
        return get(sql, provinceCode);
    }

    /**
     * 拼接查询条件
     *
     * @param dicProvinceSearchVO
     * @return
     */
    private String createSearchSQL(DicProvinceSearchVO dicProvinceSearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(dicProvinceSearchVO.getProvinceName())) {
            sql += " AND province_name LIKE '%'||:provinceName||'%' ";
        }
        return sql;
    }
}
